﻿using System;
using ZHDJ.JoinParty.BLL;
using ZHDJ.Data.Models.JoinParty;
using FineUI;
using System.Collections.Generic;
using ZHDJ.Core.Util;

namespace JoinParty.admin.FlowStep
{
    public partial class Report : PageBase
    {
        public string EditorContent
        {
            get
            {
                if (this.CurrentStepID == ProcesUtil.JoinStep38)
                    return ModuleUtil.GetUpStandCKContentByAppllicationID(this.AppllicationID);
                else
                    return string.Empty;
            }

        }
        public string ContentContent { get; set; }
        public string ResultContent { get; set; }
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }
        private void BindData()
        {

        }
        private void LoadData(string IsSave = "")
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            PartyJoinLinkExample oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);

            PartyJoinProcess ps = DB.PartyJoinProcesss.Find(this.CurrentStepID);

            string SubmitID = ModuleUtil.GetIDByAppllicationID(this.AppllicationID);
            if (!string.IsNullOrEmpty(SubmitID))
            {
                var SoModel = DB.PartyJoinSubmits.Find(SubmitID);
                if (CurrentStepID == ProcesUtil.JoinStep23 || CurrentStepID == ProcesUtil.JoinStep37 || CurrentStepID == ProcesUtil.JoinStep38)
                {
                    ContentContent = SoModel.Remark;
                }
                if (CurrentStepID == ProcesUtil.JoinStep38)
                {
                    if (!string.IsNullOrEmpty(SoModel.ResultsFindings))
                    {
                        ResultContent = SoModel.ResultsFindings;
                    }
                }
                bool?[] Checked = new bool?[13] { SoModel.Application, SoModel.ActivistsTable, SoModel.ThoughtReport, SoModel.Autobiography, SoModel.Recommending, SoModel.Train, SoModel.Careful, SoModel.Consent, SoModel.Publicity, SoModel.PublicitySituation, SoModel.AllThoughtReport, SoModel.PositiveApplication, SoModel.InspectionTable };
                int i = 0;
                foreach (var item in this.cblMaterial.Items)
                {
                    item.Selected = Convert.ToBoolean(Checked[i]);
                    i++;
                }
                foreach (var item in this.cblMaterial2.Items)
                {
                    item.Selected = Convert.ToBoolean(Checked[i]);
                    i++;
                }
            }
            SetPageShow();
            //页面只读显示
            if (this.ReadOnly.Trim() == "1")
            {
                this.btnSubmit.Visible = false;
                this.btnSaveClose.Visible = false;
                //if (IsJoinPartyAdmin)
                //{
                //    this.btnSaveClose.Visible = true;
                //}
            }
        }

        //设置页面显示内容
        public void SetPageShow()
        {

            switch (this.CurrentStepID)
            {
                case ProcesUtil.JoinStep23: break;
                case ProcesUtil.JoinStep37: this.cblMaterial2.Hidden = false; break;
                case ProcesUtil.JoinStep38:
                    this.cblMaterial2.Hidden = this.lbResult.Hidden = this.cpResult.Hidden = false;
                    //this.ckReport.Enabled = false;
                    break;
            }
        }

        private void SaveData(bool isClose, bool AdminSave = false)
        {
            string Message = "";///接收处理流程反馈的消息主要判断当前步骤是否已审核过

            bool bResult = false;
            var oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
            if (oModel != null)
            {
                string SubmitID = ModuleUtil.GetIDByAppllicationID(oModel.AppllicationID);
                var SoModel = DB.PartyJoinSubmits.Find(SubmitID);

                if (SoModel == null)
                {
                    SoModel = new PartyJoinSubmit();
                }
                if (AdminSave && oModel.Status == 1)
                {
                    oModel.Remark = this.Request.Form["editorContent"];
                    if (this.CurrentStepID == ProcesUtil.JoinStep38)
                        SoModel.ResultsFindings = this.Request.Form["editorResult"];
                    bResult = DB.SaveChanges() > 0;
                }
                else
                {
                    List<bool> check = new List<bool>();

                    foreach (var item in this.cblMaterial.Items)
                    {
                        check.Add(item.Selected);
                    }
                    SoModel.Application = check[0];
                    SoModel.ActivistsTable = check[1];
                    SoModel.ThoughtReport = check[2];
                    SoModel.Autobiography = check[3];
                    SoModel.Recommending = check[4];
                    SoModel.Train = check[5];
                    SoModel.Careful = check[6];
                    SoModel.Consent = check[7];
                    SoModel.Publicity = check[8];
                    if (this.CurrentStepID == ProcesUtil.JoinStep37 || this.CurrentStepID == ProcesUtil.JoinStep38)
                    {
                        List<bool> check2 = new List<bool>();
                        foreach (var item in this.cblMaterial2.Items)
                        {
                            check2.Add(item.Selected);
                        }
                        SoModel.PublicitySituation = check2[0];
                        SoModel.AllThoughtReport = check2[1];
                        SoModel.PositiveApplication = check2[2];
                        SoModel.InspectionTable = check2[3];
                    }
                    else
                    {
                        SoModel.PublicitySituation = false;
                        SoModel.AllThoughtReport = false;
                        SoModel.PositiveApplication = false;
                        SoModel.InspectionTable = false;
                    }
                    SoModel.ResultsFindings = this.Request.Form["editorResult"];
                    SoModel.AppllicationID = oModel.AppllicationID;
                    if (!string.IsNullOrEmpty(SoModel.ID))
                    {
                        DB.SaveChanges();
                    }
                    else
                    {
                        SoModel.ID = StringHelper.NewID;
                        DB.PartyJoinSubmits.Add(SoModel);
                        DB.SaveChanges();
                    }
                    oModel.Remark = this.Request.Form["editorContent"];
                    oModel.HandleUserID = this.CurrentUser.LoginID;
                    oModel.HandleUserName = this.CurrentUser.UserName;
                    oModel.HandleTime = DateTime.Now;
                    oModel.Status = (int)Enum_FlowStanceStatus.通过;

                    //if (this.CurrentStepID == ProcesUtil.JoinStep14 || this.CurrentStepID == ProcesUtil.JoinStep37)
                    //{
                    //    string TopBranchID = ModuleUtil.GetPartyAppointArray(this.ApplyBranchID);//申请的最高级审批组织ID

                    //    bResult = ProcesUtil.ApprovalProcesStep(oModel, out Message, TopBranchID);
                    //}
                    //else
                    //{
                    bResult = ProcesUtil.ApprovalProcesStep(oModel, out Message);
                    //}
                }
            }

            if (bResult)
            {
                if (AdminSave)
                    Alert.Show("保存成功", String.Empty, ActiveWindow.GetHideReference());
                else
                    Alert.Show("提交成功", String.Empty, ActiveWindow.GetHideReference());
            }
            else
            {
                if (!string.IsNullOrEmpty(Message))
                    Alert.Show(Message, String.Empty, ActiveWindow.GetHideReference());
            }
        }
        #endregion

        #region
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.SaveData(true);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData(true, true);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}